package com.pucong.gitee.database.backup.business.unRelational.redis.controller;

import com.pucong.gitee.database.backup.business.unRelational.redis.service.DataClusterService;
import com.pucong.gitee.database.backup.business.unRelational.redis.util.RedisNode;
import com.pucong.gitee.database.backup.common.entity.redis.KeyBean;
import com.pucong.gitee.database.backup.common.entity.redis.ZTreeBean;
import com.pucong.gitee.database.backup.common.entity.redis.dto.RenameKeyRedisDto;
import com.pucong.gitee.database.backup.utils.Res;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping("/auto/redis/dataCluster")
@Api(value = "redis dataCluster-API")
public class DataClusterController {

    @Autowired
    private DataClusterService dataClusterService;

    @GetMapping("/nodeInfo/{id}")
    @ApiOperation(value = "获取集群节点信息")
    public Res<List<RedisNode>> nodeInfo(@ApiParam("主键") @PathVariable("id") String id) {
        List<RedisNode> redisNodeList = dataClusterService.nodeInfo(id);
        return Res.ok(redisNodeList);
    }

    @GetMapping("/treeInit/{id}")
    @ApiOperation(value = "打开连接，返回数据库")
    public Res<ZTreeBean> treeInit(@ApiParam("主键") @PathVariable("id") String id) {
        ZTreeBean zTreeBean = dataClusterService.treeInit(id);
        return Res.ok(zTreeBean);
    }


    @GetMapping("/likeInit/{id}")
    @ApiOperation(value = "打开连接，返回数据库")
    public Res<ZTreeBean> likeInit(@ApiParam("主键") @PathVariable("id") String id, String pattern) {
        ZTreeBean zTreeBean = dataClusterService.likeInit(id, pattern);
        return Res.ok(zTreeBean);
    }

    @GetMapping("/treeData/{id}")
    @ApiOperation(value = "打开连接，返回数据库")
    public Res<LinkedList<ZTreeBean>> treeData(@ApiParam("主键") @PathVariable("id") String id, String pid, String pattern) {
        LinkedList<ZTreeBean> zTreeBeans = dataClusterService.treeData(id, pid, pattern);
        return Res.ok(zTreeBeans);
    }

    @GetMapping("/keysData/{id}")
    @ApiOperation(value = "打开连接，返回数据库")
    public Res<KeyBean> keysData(@ApiParam("主键") @PathVariable("id") String id, String key, String order) {
        KeyBean keyBean = dataClusterService.keysData(id, key, order);
        return Res.ok(keyBean);
    }

    @PostMapping("/renameKeyAndRetimeKey/{id}")
    @ApiOperation(value = "修改key名称和时长")
    public Res<Boolean> renameKeyAndRetimeKey(@ApiParam("主键") @PathVariable("id") String id, @Validated @RequestBody RenameKeyRedisDto dto) {
        Boolean aBoolean = dataClusterService.renameKey(id, dto.getOldKey(), dto.getNewKey());
        if(dto.getOldTime() != dto.getNewTime()) {
            Boolean aBoolean2 = dataClusterService.retimeKey(id, dto.getNewKey(), dto.getNewTime());
        }
        return Res.ok(aBoolean);
    }

    @DeleteMapping("/deleteKey/{id}")
    @ApiOperation(value = "删除key")
    public Res<Boolean> deleteKey(@ApiParam("主键") @PathVariable("id") String id, String key) {
        Boolean aBoolean = dataClusterService.deleteKey(id, id, key);
        return Res.ok(aBoolean);
    }


    @PostMapping("/updateStr/{id}")
    @ApiOperation(value = "修改字符串值")
    public Res<Boolean> updateStr(@ApiParam("主键") @PathVariable("id") String id, String key, String val) {
        Boolean aBoolean = dataClusterService.updateStr(id, key, val);
        return Res.ok(aBoolean);
    }


    @PostMapping("/insertVal/{id}")
    @ApiOperation(value = "新增值")
    public Res<Boolean> insertVal(@ApiParam("主键") @PathVariable("id") String id, int type, String key, String val, double score) {
        Boolean aBoolean = dataClusterService.insertVal(id, type, key, val, score);
        return Res.ok(aBoolean);
    }


    @DeleteMapping("/deleteVal/{id}")
    @ApiOperation(value = "删除值")
    public Res<Boolean> deleteVal(@ApiParam("主键") @PathVariable("id") String id, int type, String key, String val) {
        Boolean aBoolean = dataClusterService.deleteVal(id, type, key, val);
        return Res.ok(aBoolean);
    }

    @PostMapping("/updateVal/{id}")
    @ApiOperation(value = "修改key的值")
    public Res<List<RedisNode>> updateVal(@ApiParam("主键") @PathVariable("id") String id, String json) {
        Boolean aBoolean = dataClusterService.updateVal(id, json);
        return Res.ok(aBoolean);
    }


    @PostMapping("/insertKey/{id}")
    @ApiOperation(value = "新增key")
    public Res<Boolean> insertKey(@ApiParam("主键") @PathVariable("id") String id, int type, String key, String val, long time, double score) {
        Boolean aBoolean = dataClusterService.insertKey(id, type, key, val, time, score);
        return Res.ok(aBoolean);
    }


    @DeleteMapping("/removeKey/{id}")
    @ApiOperation(value = "删除所有key")
    public Res<Boolean> removeKey(@ApiParam("主键") @PathVariable("id") String id) {
        Boolean aBoolean = dataClusterService.removeKey(id);
        return Res.ok(aBoolean);
    }

    @PostMapping("/backupDb/{id}")
    @ApiOperation(value = "备份数据")
    public Res<Boolean> backupKey(@ApiParam("主键") @PathVariable("id") String id, String pattern) {
        Boolean aBoolean = dataClusterService.backupKey(id, pattern);
        return Res.ok(aBoolean);
    }

    @PostMapping("/recoverDb/{id}")
    @ApiOperation(value = "还原数据")
    public Res<Boolean> recoveKey(@ApiParam("主键") @PathVariable("id") String id, String fileId) {
        Boolean aBoolean = dataClusterService.recoveKey(id, fileId);
        return Res.ok(aBoolean);
    }
}
